#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <unistd.h>
#include <fcntl.h>
#include "keycode.h"

/*
 * read file of group.cfg, fill struct CameraGroup.
 */
static int get_group_param(unsigned char * file, 
							grouplist * head,		/* 初始化头 */
							grouplist * newnode)	/* 节点 */
{
	FILE *fp;	
	char buf[128];
	int count = 0, in = 0;
	char *pdata[32];
	char *pbuf;

	fp = open_file(file);
	while (!feof(fp)) {
		memset(buf, 0, sizeof(buf));
		memset(pdata, 0, sizeof(pdata));
		fscanf(fp, "%s", buf);
		if (buf[0] == '#'||buf[0] == ' ')
			continue;

		if (strlen(buf) < 4)
			continue;

		pbuf = buf;
		in = 0;
		while ((pdata[in] = strtok(pbuf, ",")) != NULL) {
			in++;
			pbuf = NULL; 
		}

		if (pdata[1] == NULL)
			continue;
		
		NEW_LIST_NODE(grouplist, newnode);	/* malloc分配内存 */
		newnode->group.Id	   = atoi(pdata[0]);
		newnode->group.GroupNo = atoi(pdata[1]); 
		newnode->group.CameraNo = atoi(pdata[2]);
		newnode->group.PresetNo = atoi(pdata[3]);
		newnode->group.StopTime = atoi(pdata[4]);
		list_add_tail(&newnode->list, &head->list);	/* 插入头的后面 */

		count++;
	}
	fclose(fp);

	return count;
}

void print_result(grouplist *posnode)
{
	#ifndef DEBUG
	printf("\tGroup Id = %d\n", posnode->group.Id);
	printf("\tGroupNo = %d\n", posnode->group.GroupNo);
	printf("\tCameraNo = %d\n", posnode->group.CameraNo);
	printf("\tPresetNo = %d\n", posnode->group.PresetNo);
	printf("\tStopTime = %d\n\n", posnode->group.StopTime);
	#endif
}

/*
 * 搜索单个轮巡组号
 *
 * s_head: 表头
 * s_newnode : 新节点
 * num : 轮巡组号.
 */
int get_signle_group(grouplist * s_head, grouplist * s_newnode, int num)
{
	grouplist *head, *newnode, *posnode;
	struct list_head *pos;
	int count = 0;
	
	NEW_LIST_NODE(grouplist, head);	/* malloc分配内存 */
	INIT_LIST_HEAD(&head->list);	/* 初始化头 */

	// 存取全部group数据.
	get_group_param(GROUP_FILE, head, newnode);

	list_for_each(pos, &(head->list)) {
		posnode = list_entry(pos, grouplist, list);		
				
		// 存取轮巡单组数据
		if (posnode->group.GroupNo == num) {
			NEW_LIST_NODE(grouplist, s_newnode);
			s_newnode->group = posnode->group;
			list_add_tail(&s_newnode->list, &s_head->list);
			count++;
		}
	}

	// 释放存取全部group数据的双链表
	FREE_LIST(grouplist, head, list);

	return count;
}

/*
 * 搜索ID号
 *
 * s_head: 表头
 * s_newnode : 新节点
 * id : id号.
 */
int get_signle_group_id(grouplist * s_head, grouplist * s_newnode, int id)
{
	grouplist *head, *newnode, *posnode;
	struct list_head *pos;
	int count = 0;
	
	NEW_LIST_NODE(grouplist, head);
	INIT_LIST_HEAD(&head->list);

	// 存取全部group数据.
	get_group_param(GROUP_FILE, head, newnode);

	list_for_each(pos, &(head->list)) {
		posnode = list_entry(pos, grouplist, list);		
				
		// 存取轮巡单组数据
		if (posnode->group.Id == id) {
			NEW_LIST_NODE(grouplist, s_newnode);
			s_newnode->group = posnode->group;
			list_add_tail(&s_newnode->list, &s_head->list);
			count++;
		}
	}

	// 释放存取全部group数据的双链表
	FREE_LIST(grouplist, head, list);

	return count;
}

/*
 * 通过 ID 号找到group组号
 */
int GetGroupNoByID(int id)
{
	grouplist *head, *newnode, *posnode;
	struct list_head *pos;
	int count = 0;
	int result = -1;
	
	NEW_LIST_NODE(grouplist, head);
	INIT_LIST_HEAD(&head->list);

	// 存取全部group数据.
	get_group_param(GROUP_FILE, head, newnode);

	list_for_each(pos, &(head->list)) {
		posnode = list_entry(pos, grouplist, list);		
				
		// 存取轮巡单组数据
		if (posnode->group.Id == id) {
			result = posnode->group.GroupNo;
			break;
		}
	}

	// 释放存取全部group数据的双链表
	FREE_LIST(grouplist, head, list);

	return result;
}

/*
 * 存入动态数组
 * num : 轮巡编号
 * len : 轮巡组长度
 */
grouplist ** group_item(int num, int * len)
{
	int count;
	int i = 0;
	grouplist *head, *newnode, *posnode;
	struct list_head *pos;
	grouplist ** gp;

	NEW_LIST_NODE(grouplist, head);
	INIT_LIST_HEAD(&head->list);

	count = get_signle_group(head, newnode, num);
	*len = count;
	if (count == 0)	{
		// 释放存取数据的双链表
		FREE_LIST(grouplist, head, list);
		return NULL;
	}

	gp = (grouplist **)malloc(sizeof(grouplist) * count);
	if (gp != NULL) {
		list_for_each(pos, &(head->list)) {
			gp[i] = list_entry(pos, grouplist, list);
			//print_result(gp[i]);
			i++;
		}
	}

	// 释放存取数据的双链表
	FREE_LIST(grouplist, head, list);

	return gp;
}

#if 0
/* modified 2012/9/6 10:44, malloc成对出现,对之前的代码修改 */
grouplist ** _group_item(int num, grouplist ** gp, int * len)
{
	int count;
	int i = 0;
	grouplist *head, *newnode, *posnode;
	struct list_head *pos;
	//grouplist ** gp;

	NEW_LIST_NODE(grouplist, head);
	INIT_LIST_HEAD(&head->list);

	count = get_signle_group(head, newnode, num);
	*len = count;
	if (count == 0)	{
		// 释放存取数据的双链表
		FREE_LIST(grouplist, head, list);
		return NULL;
	}

	//gp = (grouplist **)malloc(sizeof(grouplist) * count);
	if (gp != NULL) {
		list_for_each(pos, &(head->list)) {
			gp[i] = list_entry(pos, grouplist, list);
			//print_result(gp[i]);
			i++;
		}
	}

	// 释放存取数据的双链表
	FREE_LIST(grouplist, head, list);

	return gp;
}



void example2()
{
	grouplist **ret;
	int i;
	int len = 0;

	ret = group_item(2, &len);

	for (i = 0; i < len; i++)	{
		print_result(ret[i]);
	}

	free(ret); 
}

void example()
{
	grouplist **ret;
	int i;
	int len = 0;

	ret = group_item(2, &len);

	for (i = 0; i < len; i++)	{
		print_result(ret[i]);
	}

	free(ret); 
}

int main()
{
	example();
}


int _main()
{
	grouplist *s_head, *s_newnode, *s_posnode;
	struct list_head *s_pos;

	NEW_LIST_NODE(grouplist, s_head);
	INIT_LIST_HEAD(&s_head->list);

	get_signle_group(s_head, s_newnode, 2);

	list_for_each(s_pos, &(s_head->list)) {
		s_posnode = list_entry(s_pos, grouplist, list);
		print_result(s_posnode);
	}
	
	// 释放存取单个group数据的双链表
	FREE_LIST(grouplist, s_head, list);

	return 0;
}
#endif
// gcc -o group group.c readcfg.c 
